//
//  TopicsViewController.m
//  觅影
//
//  Created by Zeus on 16/4/8.
//  Copyright © 2016年  陶虹. All rights reserved.
//

#import "TopicsViewController.h"
#import "TopicsTableViewCell.h"
#import "TopicsMainModel.h"
#import "CommentViewController.h"

@interface TopicsViewController () <UITableViewDataSource, UITableViewDelegate>

@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property(nonatomic, strong)NSMutableArray *modelArray;
@property(nonatomic, assign)NSInteger size;
@end

@implementation TopicsViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.navigationController.navigationBar.translucent = NO;
    self.navigationItem.title = @"话题";
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.rowHeight = 50;
    [self.tableView registerNib:[UINib nibWithNibName:@"TopicsTableViewCell" bundle:nil]forCellReuseIdentifier:@"TopicsCell"];
    
    [self getData];
    // 下拉刷新
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [self getData];
    }];
    
    // 上拉加载更多
    self.tableView.mj_footer = [MJRefreshAutoFooter footerWithRefreshingBlock:^{
        [self loadMore];
    }];
    
    
    self.size = 18;
}

#pragma mark --- 加载更多 ---
- (void)loadMore
{
    self.size += 18;
    [RequestManager requestWithUrl:[NSString stringWithFormat:@"http://static.v6v7.com/snowman_api/movie_traces/topic_list?page=1&page_size=%ld",self.size] requestType:RequestTypeGET parDic:nil finshed:^(NSData *data) {
        
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        NSDictionary *resultDic = dic[@"result"];
        NSArray *dataArray = resultDic[@"data"];
        self.modelArray = [NSMutableArray array];
        for (NSDictionary *dataDic in dataArray) {
            TopicsMainModel *model = [[TopicsMainModel alloc]init];
            [model setValuesForKeysWithDictionary:dataDic];
            [self.modelArray addObject:model];
        }
        dispatch_async(dispatch_get_main_queue(), ^{
            [self.tableView reloadData];
            [self.tableView.mj_footer endRefreshing];
                   });
        
    } error:^(NSError *error) {
        NSLog(@"请求错误");
    }];

}


#pragma mark --- 请求数据 ---
- (void)getData
{
    [RequestManager requestWithUrl:@"http://static.v6v7.com/snowman_api/movie_traces/topic_list?page=1&page_size=18" requestType:RequestTypeGET parDic:nil finshed:^(NSData *data) {
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        NSDictionary *resultDic = dic[@"result"];
        NSArray *dataArray = resultDic[@"data"];
        self.modelArray = [NSMutableArray array];
        for (NSDictionary *dataDic in dataArray) {
            TopicsMainModel *model = [[TopicsMainModel alloc]init];
            [model setValuesForKeysWithDictionary:dataDic];
            model.myId = dataDic[@"id"];
            [self.modelArray addObject:model];
        }
        dispatch_async(dispatch_get_main_queue(), ^{
            [self.tableView reloadData];
            [self.tableView.mj_header endRefreshing];
        });
        
    } error:^(NSError *error) {
        NSLog(@"请求错误");
    }];

}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.modelArray.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    TopicsMainModel *model = self.modelArray[indexPath.row];
    if ([model.poster isEqualToString:@""]) {
        
        if ([model.is_hot integerValue] == 0) {
            TopicsTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"TopicsCell" forIndexPath:indexPath];
            cell.model = model;
            [cell.commentButton addTarget:self action:@selector(commentButtonAction:) forControlEvents:UIControlEventTouchUpInside];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            cell.titleX.constant = 15;
            cell.hotImageView.hidden = YES;
             cell.picImageView.hidden = YES;
            return cell;
        }
        else
        {
            TopicsTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"TopicsCell" forIndexPath:indexPath];
            cell.model = model;
            [cell.commentButton addTarget:self action:@selector(commentButtonAction:) forControlEvents:UIControlEventTouchUpInside];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            cell.hotX.constant = 15;
            cell.titleX.constant = 48;
            cell.hotImageView.hidden = NO;
             cell.picImageView.hidden = YES;
            return cell;
        }
    }
    else
    {
        TopicsTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"TopicsCell" forIndexPath:indexPath];
        cell.picImageView.hidden = NO;
        cell.model = model;
        [cell.commentButton addTarget:self action:@selector(commentButtonAction:) forControlEvents:UIControlEventTouchUpInside];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        if ([model.is_hot integerValue] == 0) {
            cell.titleX.constant = 50;
            cell.hotImageView.hidden = YES;
            return cell;
        }
        else
        {
            TopicsTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"TopicsCell" forIndexPath:indexPath];
            cell.model = model;
            [cell.commentButton addTarget:self action:@selector(commentButtonAction:) forControlEvents:UIControlEventTouchUpInside];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            cell.hotImageView.hidden = NO;
            return cell;
        }
        
    }

    
}

#pragma mark --- 评论按钮的点击事件 ---
- (void)commentButtonAction:(UIButton *)button
{
    TopicsTableViewCell *cell = (TopicsTableViewCell *)button.superview.superview;
    NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
    TopicsMainModel *model = self.modelArray[indexPath.row];
    NSLog(@"%ld",indexPath.row);
    CommentViewController *commentVC = [[CommentViewController alloc]init];
    commentVC.myId = model.myId;
    NSLog(@"==============%@",model.myId);
    self.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:commentVC animated:YES];
    self.hidesBottomBarWhenPushed = NO;
}




@end
